AQUA.Windows.SecurityRoles = {
	ajax_url		: "mod_settings/security_roles.php",
	content_url		: "ui/mod_settings/security_roles.html",
	title           : "Security Roles",
	context			: "#security-roles-div",
	server_call		: "",
	data			: {},
	is_loaded		: false,
	show			: _form_base_show,
	hide			: _form_base_hide,
	serialize		: _form_serialize,
	set_fields		: _form_set_values,
	field			: _form_get_field,
	number_dropdown	: _build_number_dropdown,
	data_dropdown	: _build_data_dropdown,
	checkbox		: _set_check_box,
	
	_show: function(){
		var _this = this;
		
		$(this.context).find("#security_role")
			.unbind("change")
			.change(function(){
				_this.refresh();
			});
		
		$(this.context).find("#chk_select_clear_all")
			.unbind("click")
			.click(function(){
				_this.clear_select_all();
			});
		
		this.field("name").val("");
		this.data_dropdown("security_role", cache.security_roles, null, null, true);
		
		this.create_permission_list();
		show_feedback("Load complete");
	},
	
	refresh: function(){
		var _this = this;
		var key = this.field("security_role").val();
		this.field("name").val("");
		this.field("ranking").val("");
		console.log("ROLE: "+key);
		$(cache.security_roles).each(function(){
			console.log(this);
			if (key == this.id){
				_this.field("name").val(unescape(this.name));
				_this.field("ranking").val(this.ranking);
			}
		});
		
		set_title("Loading Details");
		this.serialize("security_roles_read_one");
		get_server_data(this, function(data){
			//FIRST RESET ALL CHECK-BOXES
			$(_this.context).find("#audit_table input[type=checkbox]").each(function(){
				this.checked = false;
			});
			
			$(data).each(function(){
				var item = this;
				$(_this.context).find("#audit_table input[type=checkbox]").each(function(){
					if (this.audit_id == item.id && item.can_perform == 1)
						this.checked = true;
				});
			});
			
			show_feedback("Load complete");
		});
		
	},
	
	create_permission_list: function(){
		hide_title();
		var _this = this;
		var col_count = 0;
		$(this.context).find("#audit_table").children().remove();
		$(cache.security_types).each(function(){
			if (col_count == 0 || col_count%3 == 0){
				row		= $("<tr>");
				$(_this.context).find("#audit_table").append(row);
			}
			
			var col = $("<td>").appendTo(row);
			
			var check	= $("<input type=checkbox>");
			
			check
				.prop("audit_id", this.id)
				.appendTo(col);
			
			col.append("<span>"+unescape(this.name)+"</span>");
			col
				.hover(
					function(){
						$(this).addClass("ui-state-hover");
						$(this).css("cursor", "pointer");
					}
					, function(){
						$(this).removeClass("ui-state-hover");
						$(this).css("cursor", "default");
					}
				)
				.unbind("click")
				.click(function(){
					if (check.attr("checked"))
						check.removeAttr("checked");
					else
						check.attr("checked", "checked");
				});
			col_count++;
		});
		
		update_table_format($(this.context).find("#audit_table").parents("table"));
	},
	
	clear_select_all: function(){
		var _this = this;
		$(this.context).find("#audit_table input[type=checkbox]").each(function(){
			this.checked = $(_this.context).find("#chk_select_clear_all").is(":checked");
		});
	},
	
	update: function(){
		var _this = this;
		set_title("Updating");
		this.serialize("security_roles_update");
		this.data.audit_types = [];
		
		$(this.context).find("#audit_table input[type=checkbox]").each(function(){
			if (true == this.checked){
				_this.data.audit_types.push(this.audit_id);
			}
		});
		
		get_server_data(this, function(data){
			show_feedback("Action completed");
		});
	},
		
	create: function(){
		var _this = this;
		edit_security_role.type = "create";
		edit_security_role.eventName = null;
		$().lookup_form(edit_security_role, function(data){
			cache.refresh();
			var item = data[0];
			_this.field("security_role")
				.append("<option value='"+item.id+"'>"+unescape(item.name)+"</option>");
			show_feedback("Action completed");
		});
	},
		
	remove: function(){
		var _this = this;
		yesno = confirm("Are you sure you want to remove this item?");
		if (false == yesno) return;
		
		set_title("Removing...");
		this.serialize("security_roles_remove");
		get_server_data(this, function(data){
			cache.refresh();
			var key = _this.field("security_role").val();
			$(_this.context).find("#security_role option[value="+key+"]").remove();
			
			_this.set_fields();
			show_feedback("Action completed");
		});
	}
};


